import React from 'react';
import {Button, message, Modal} from 'antd';
import {WeaLocaleProvider, WeaTools} from "ecCom";
import loadjs from 'loadjs';

const getLabel = WeaLocaleProvider.getLabel;
const confirm = Modal.confirm;

export default class FormSignIndex extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            // src: "http://app148.qiyuesuo.net/sign/2705982712687689870?hide_menu=true&signedByEc=1&em_client_type=mobile",
            // src: "http://127.0.0.1:8080/docs/qiyuesuo.html",
            src: "",
            nodeid: 0,
            requestid: 0
        };
    }

    componentDidMount() {
        this.registered();

        const {location = {}} = this.props;
        let {search = {}, query} = location;//默认值，签署
        if(!(query && query.url)){
            query = this.getQueryString(search) || {};
        }
        if(query && query.requestid) {
            this.setState({requestid: query.requestid});
        }
        if(query && query.nodeid) {
            this.setState({nodeid: query.nodeid});
        }
        if (query && query.url) {//如果直接传递url解析
            this.setState({src: decodeURIComponent(query.url)});
        } else {//重新去后台获取url
            let formWindow = this.getFormWindwo();
            let wfform = formWindow && formWindow.wfform || null;
            const baseInfo = wfform && wfform.getBaseInfo() || {};//获取表单默认参数，支持参数覆盖
            baseInfo.requestid && this.setState({requestid: baseInfo.requestid});
            baseInfo.nodeid && this.setState({nodeid: baseInfo.nodeid});
            query = {method: "sign", ...baseInfo, ...query}
            WeaTools.callApi((window.ecologyContentPath || "") + "/mobile/plugin/qiyuesuo/wf_qiyuesuoAjax.jsp", "GET", query).then(res => {
                const {url, message: msg = ""} = res;
                if (url) {
                    this.setState({src: url});
                } else {
                    message.error(msg, 3);
                }
            })
        }
    }

    componentWillUnMount() {
        var PMUtil = null;
        if (window && window.PostMessageUtil) {
            PMUtil = window.PostMessageUtil;
        }
        PMUtil && PMUtil.removeListener(window)
    }

    render() {
        const {src} = this.state;
        let content = null;
        if (src) {
            content = <iframe style={{border: "none"}} width={"100%"} height={"100%"} src={src}></iframe>;
        }
        return <div style={{height: "100%", width: "100%", "background-color": "#f4f4f4"}}>
            {content}
        </div>
    }

    registered = () => {
        const init = () => {
            this.setState({hasLoad: true});
            const {src, requestid, nodeid} = this.state;
            //需要获取wfform
            let wfForm = null, PMUtil = null;
            let wformWindow = this.getFormWindwo();
            if (wformWindow) {
                wfForm = wformWindow.wfform;
            }
            if (window && window.PostMessageUtil) {
                PMUtil = window.PostMessageUtil;
            }
            PMUtil && PMUtil.resisterListener(window, wfForm, src, function (data, autoSubmitAfterSigned, message) {
                if(autoSubmitAfterSigned && autoSubmitAfterSigned == 2) {
                    confirm({
                        title: getLabel(15172, "系统提示"),
                        content: message ? message : "您已完成签署，是否提交流程？",
                        onOk() {
                            wfForm && wfForm.doSubmit();
                            try {
                                wfForm && wfForm.switchTab && wfForm.switchTab("form")
                                window.top.close();
                            } catch (e) {
                                window.close();
                            }
                        },
                        onCancel() {

                        }
                    });
                } else {
                    wfForm && wfForm.doSubmit();
                    try {
                        wfForm && wfForm.switchTab && wfForm.switchTab("form")
                        window.top.close();
                    } catch (e) {
                        window.close();
                    }
                }

            }, requestid, nodeid);
        }
        loadjs && loadjs((window.ecologyContentPath || "") + "/js/jquery/jquery_wev8.js", init);
        loadjs && loadjs((window.ecologyContentPath || "") + "/workflow/qiyuesuo/js/wf_qiyuesuoPostMessage.js", init);
    }

    //兼容
    getFormWindwo = () => {
        let formwindow = null;
        if (!formwindow && window.opener && window.opener.wfform) {
            formwindow = window.opener
        }
        if (!formwindow && window.parent && window.parent.wfform) {
            formwindow = window.parent
        }
        if (!formwindow && window.wfform) {
            formwindow = window;
        }
        return formwindow;
    }

    getQueryString = (str) => {
        if(!str){
            return {};
        }
        const obj = {};
        str.substr(1).split('&').map(item=>{
            let arr = item.split('=');
            obj[arr[0]]=arr[1]
        })
        return obj
    }
}
